/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MLEVList.h"

//---------------------------------------------------------------------------

MLEVList::MLEVList(void)
{
}

MLEVList::~MLEVList(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MLEVList::CleanUp(void)
{
	MLEVRecord *elem;

	for(iter=theVector.begin(); iter!=theVector.end(); iter++){
		elem = *iter;
		if(elem!=NULL){
			*iter = NULL;
			delete elem;
		}
	}
	theVector.clear();
}

//---------------------------------------------------------------------------

MLEVRecord* MLEVList::CreateRecord(void)
{
	MLEVRecord* elem;

	try{
		elem = new MLEVRecord;
	}
	catch(...){
		elem = NULL;
	}
	
	return elem;
}

//---------------------------------------------------------------------------

MLEVRecord* MLEVList::FindRecord(MLEVRecord* elemIn)
{
	MLEVRecord* elem;
	MLEVRecord* res;

	if(elemIn==NULL) return NULL;

	res = NULL;
	for(iter=theVector.begin(); iter!=theVector.end() && res==NULL; iter++){
		elem = *iter;
		if(elem!=NULL){
			if(elem->xEvent==elemIn->xEvent)
				if(elem->xLength==elemIn->xLength)
					if(elem->xValue[0]==elemIn->xValue[0])
						if(elem->xValue[1]==elemIn->xValue[1])
							res = elem;
		}
	}

	return res;
}

//---------------------------------------------------------------------------

bool MLEVList::Add(MLEVRecord* elemIn)
{
	MLEVRecord* elem;
	int cnt;

	if(elemIn==NULL) return true;	// nothing to add

	elem = CreateRecord();
	if(elem==NULL) return false;

	elem->LoadFromPointer(elemIn);

	cnt = (int)theVector.size();
	theVector.insert(theVector.begin(), elem);
	cnt = (int)theVector.size() - cnt;
	if(cnt!=1){
		delete elem;
		return false;
	}

	return true;
}

bool MLEVList::AddNoDuplicates(MLEVRecord* elemIn)
{
	MLEVRecord* elem;

	elem = FindRecord(elemIn);
	if(elem!=NULL) return true;

	return Add(elemIn);
}

void MLEVList::Clear(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

int MLEVList::GetSize(void)
{
	return (int)theVector.size();
}

MLEVRecord* MLEVList::GetByIdx(int idxIn)
{
	if(idxIn<0)          return NULL;
	if(idxIn>=GetSize()) return NULL;

	return theVector[idxIn];
}

//---------------------------------------------------------------------------
